<?php
namespace app\admin\controller;

use think\Request;
use think\Validate;

/**
 * admin
 * jiangong#888
 */
class Hunanconstr extends Base
{
    // 增加集团领导、集团部室、分公司领导绑定项目
    public function test()
    {
        $post = request()->only(['project_id', 'company_id'], 'get');
        
        if (count($post) != 2) {
            echo 'error';
            exit();
        }
        
        // 项目ID
        $resInsertId = $post['project_id'];
        $post['company_id'] = $post['company_id'];
        
        // 邀请码处理
        // 如果是集团领导增加绑定记录
        $field = 'user_id, head_img, name, tel, position, type, code';
        
        $leaderGroup = db('wx_jg_bound')->field($field)
            ->where(['type'=>['in', '1,2']])
            ->group('user_id')
            ->select();
        
        dump($leaderGroup);
            
        if ($leaderGroup) {
            $leaderData = [];
            
            foreach ($leaderGroup as $key=>$value) {
                $leaderData[$key] = $value;
                $leaderData[$key]['item_id'] = $resInsertId;
                $leaderData[$key]['create_time'] = $this->timeStamp;
                
                try {
                    db('wx_jg_bound')->insert($leaderData[$key]);
                } catch (\Exception $e) {
                    echo $e->getMessage();
                }
            }
            
            
        }
        
        // 如果是分公司领导该公司下增加绑定记录
        $field = 'b.user_id, b.head_img, b.name, b.tel, b.position, b.type, b.code';
                                
        $leaderFiliale = db('wx_jg_bound')
            ->field($field)
            ->alias('b')
            ->join('project p', 'b.item_id = p.id')
            ->where(['b.type'=>3, 'p.company_id'=>$post['company_id']])
            ->group('b.user_id')
            ->select();
        
        dump($leaderFiliale);
        
        if ($leaderFiliale) {
            $filialeData = [];
            
            foreach ($leaderFiliale as $key=>$value) {
                $filialeData[$key] = $value;
                $filialeData[$key]['item_id'] = $resInsertId;
                $filialeData[$key]['create_time'] = $this->timeStamp;
                
                try {
                    db('wx_jg_bound')->insert($filialeData[$key]);
                } catch (\Exception $e) {
                    echo $e->getMessage();
                }
            }
            
        }
        
    }
    
    // 首页框架
    public function index()
    {
        if ( ! session('?user_id')) {
            $this->redirect('admin/Hunanconstr/login');
        }
        
        return $this->fetch();
    }
    
    //批量轮播图上传
    public function List_img_uploads($name, $sk, $request)
    {
        $path = 'public' . DS;
        $paths = 'uploads' . DS . $sk . DS . 'admin' . '/';
        $img['0'] = request()->file($name . '1');
        $img['1'] = request()->file($name . '2');
        $img['2'] = request()->file($name . '3');
        foreach ($img as $key=>$file) {
            if ( ! empty($file)) {
                $info = $file->move(ROOT_PATH . $path . $paths);
                if ($info) {
                    $imgs[$key] = $paths . $info->getSaveName();
                } else {
                    err('', $file->getError());
                }
            } else {
                $imgs[$key] = "";
            }
        }
        //返回数据
        $mg = json_encode($imgs);
        return $mg;
    }
    
    ##分解轮播
    public function resolve_imgs($images, $new_image)
    {
        $images = json_decode($images); //查询数据库有图片的情况下，转换成json格式
        $new_image = json_decode($new_image); //把新图片替换成旧图片
        //图片替换操作
        if ( ! empty($images)) {
            foreach ($new_image as $key=>$value) {
                //判断图片不为空的情况下修改
                if ($new_image[$key] != null && $new_image != "") {
                    $images[$key] = $new_image[$key];
                } else { //保持原有图片格式
                }
            }
        } else {
            $images = $new_image; //如果图片为空的话就直接用新图片替换成旧图片
        }
        
        $images = json_encode($images);
        return $images;
    }
    
    // 登录
    public function login()
    {
        if (request()->isPost()) {
            $post = request()->only(['account', 'pwd'], 'post');
            
            if (empty($post['account']) or empty($post['pwd'])) {
                err('', '账号或密码错误');
            }
            
            $result = db('customer')->where(['username'=>$post['account'], 'id'=>1000])->field("id, mobile, username, name, pwd, status, expire, rules")->find();
            
            // 查找不到
            if ( ! $result) {
                err('', '账号与密码不匹配');
            }
            
            if ($result['status'] != 1) {
                err('', '账号已禁用，请联系管理员');
            }
            
            // 密码不正确
            if (md5($post['pwd']) != $result['pwd']) {
                err('', '账号与密码不匹配');
            }
            
            $rules = explode(',', $result['rules']);
            session('user_id', $result['id']);
            session("user_account", $result['username']);
            session('user_name', $result['name']);
            session('user_auth', $rules);
            
            // 验证成功
            suc('', '验证成功');
        }
        
        // 默认登录失败
        return $this->fetch();
    }
    
    // 验证码检测
    public function check($code = '')
    {
        $captcha = new \think\captcha\Captcha();
        if ( ! $captcha->check($code)) {
            $this->error('验证码错误,请从新输入....');
        }
    }
    
    // 登出
    public function logout()
    {
        session('user_id', null);
        session('user_account', null);
        session('user_name', null);
        session('user_auth', null);
        return $this->redirect('admin/Hunanconstr/login');
    }
    
    // 欢迎页面
    public function welcome()
    {
        return $this->fetch();
    }

    /**
     * 员工管理
     * **/
    //员工列表
    public function employee_list()
    {
        $res = db('wx_jg_personal')->order('create_time desc')->paginate($this->page, false);
        
        // 员工总人数
        $total = db('wx_jg_personal')->count();
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('total', $total);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //员工职位编辑
    public function employee_edit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['position'] = $post['position'];
            $data['company_id'] = $post['company_id'];
            $res = db('wx_jg_personal')->where(['id'=>$id])->update($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/employee_list';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/employee_list';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        //员工信息
        $res = db('wx_jg_personal')->field('id,position,company_id')->where(['id'=>$id])->find();
        //所有职位
        $framework = db('wx_jg_framework')->field('type,position_name')->select();
        //所以企业
        $company = db('company')->field('id,company_name')->where(['status'=>1, 'state'=>1])->select();
        $this->assign('res', $res);
        $this->assign('framework', $framework);
        $this->assign('company', $company);
        return $this->fetch();
    }
    
    //员工删除
    public function employee_del($id)
    {
        $res = db('wx_jg_personal')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/employee_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/employee_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //用户列表
    public function UserList()
    {
        $where['admin_id'] = $this->accountId;
        $res = db('applet_user')->where($where)->paginate($this->page, false);
        
        // 用户总数
        $total = db('applet_user')->count();
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        $this->assign('total', $total);
        return $this->fetch();
    }
    
    //用户删除
    public function UserDel($id)
    {
        $res = db('applet_user')->where(['id'=>$id, 'admin_id'=>$this->accountId])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/UserList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/UserList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }

    /**
     * 企业管理
     * **/
    //公司列表
    public function company_list()
    {
        $res = db('company')->alias('a')
            ->join('wx_jg_binding b', 'a.code = b.code', 'left')
            ->field('a.id,a.company_name,a.company_logo,a.code,a.create_time,
                        a.update_time,a.status,a.sort,a.state,b.name,a.payment,a.amount_all,a.item_number')
            ->order('a.sort asc')
            ->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //公司增加
    public function company_add()
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            $data['company_name'] = $post['company_name'];
            $data['status'] = $post['status'];
            //图片上传
            $file = new \app\admin\logic\File();
            $img = $file->uploadFileAdmin($admin_id);
            if ( ! empty($img)) {
                $data['company_logo'] = IMG_ADMIN . $img;
            }
            $data['create_time'] = time();
            $data['update_time'] = time();
            
            $res = db('company')->insert($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/company_list';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/company_list';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        return $this->fetch();
    }
    
    //公司编辑
    public function company_edit($id)
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            $data['company_name'] = $post['company_name'];
            $data['status'] = $post['status'];
            
            //图片上传
            $file = new \app\admin\logic\File();
            $img = $file->uploadFileAdmin($admin_id);
            if ( ! empty($img)) {
                $data['company_logo'] = IMG_ADMIN . $img;
            }
            
            $data['update_time'] = time();
            $data['item_number'] = $post['item_number'];
            $data['code'] = $post['code'];
            $data['amount_all'] = $post['amount_all'];
            $data['payment'] = $post['payment'];
            $res = db('company')->where(['id'=>$id])->update($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = url('admin/Hunanconstr/company_list', ['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = url('admin/Hunanconstr/company_list', ['id'=>$id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        $where['id'] = $id;
        $res = db('company')->where($where)->find();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //公司删除
    public function company_del($id, $type)
    {
        $res = db('company')->where(['id'=>$id])->update(['state'=>$type]);
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = url('admin/Hunanconstr/company_list', ['id'=>$id]);
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = url('admin/Hunanconstr/company_list', ['id'=>$id]);
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }

    /**
     * 工程管理
     * **/
    //项目列表
    public function project_list()
    {
        $field = ['project_name', 'company_id', 'pact_status', 'project_status'];
        $post = request()->only($field, 'post');
        
        $where = [];
        
        // 搜索
        foreach ($post as $key=>$value) {
            if ( ! empty($value) or $value === '0') {
                $where[$key] = $value;
            }
        }
        
        if ( ! empty($post['project_name'])) {
            $where['project_name'] = ['like', '%' . $post['project_name'] . '%'];
        }
        
        $where['review_status'] = 1;
        $where['status'] = ['neq',  - 1];
        
        $res = db('project')->where($where)->order('project_no')->paginate($this->page, false);
        
        // 获取统计数量
        $arr = db('project')->field('count(*) as number,sum(plan_output_total) as plan_output_total,sum(total_area) as total_area')->where($where)->find();
        
        // 已开工项目
        $projectsWhere = $where;
        $projectsWhere['start_work'] = 1;
        $arra = db('project')->field('count(*) as number')->where($projectsWhere)->find();
        
        // 正常的项目
        $projectsWhere = $where;
        $projectsWhere['project_status'] = ['neq',  - 1];
        
        if ( ! empty($post['project_status'])) {
            if ($post['project_status'] === '0') {
                $projectsWhere['project_status'] = $post['project_status'];
            }
        }
        
        $normalProject = db('project')->field('count(*) as number')->where($projectsWhere)->find();
        
        // 滞后项目
        $lagProject = $arr['number'] - $normalProject['number'];
        
        // 查询公司
        $company = db('company')->field('id, company_name')
            ->where(['state'=>1])
            ->order('id')
            ->limit(100)
            ->select();
        
        // 搜索赋值
        foreach ($field as $key=>$value) {
            if ( ! isset($post[$value])) {
                $post[$value] = '';
            }
        }
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('arr', $arr);
        $this->assign('arra', $arra);
        $this->assign('arraa', $normalProject);
        $this->assign('lagProject', $lagProject);
        $this->assign('company', $company);
        $this->assign('page', $page);
        $this->assign('search', $post);
        return $this->fetch();
    }
    
    //项目审核
    public function examine($id, $type)
    {
        $res = db('project')->where(['id'=>$id])->update(['review_status'=>$type]);
        if ($res and $type == 1) {
            //如果审核通过，创建绑定账户
            $arr = db('project')->field('add_uid,code')->where(['id'=>$id])->find();
            //获取个人信息
            $name = db('applet_user')->field('avatarurl,nickname,mobile')->where(['id'=>$arr['add_uid']])->find();
            $data = [];
            $data['user_id'] = $arr['add_uid'];
            $data['item_id'] = $id;
            $data['position'] = "普通员工";
            $data['code'] = $arr['code'];
            $data['head_img'] = $name['avatarurl'];
            $data['name'] = $name['nickname'];
            $data['tel'] = $name['mobile'];
            $data['create_time'] = time();
            $str = db('wx_jg_bound')->insert($data);
        } elseif ($res and $type == 2) {
            //如果审核通过，创建绑定账户
            $arr = db('project')->field('add_uid,code')->where(['id'=>$id])->find();
            //获取个人信息
            $name = db('applet_user')->field('avatarurl,nickname,mobile')->where(['id'=>$arr['add_uid']])->find();
            $str = db('wx_jg_bound')->where(['user_id'=>$arr['add_uid'], 'item_id'=>$id])->delete();
        }
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //项目增加
    public function project_add()
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            //判断用户ID是否存在
            $add_uid = db('applet_user')->where(['admin_id'=>1000, 'id'=>$post['add_uid']])->find();
            if ( ! $add_uid) {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '创建用户ID不存在';
                $data['url'] = 'admin/Hunanconstr/project_add';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data['add_uid'] = $post['add_uid'];
            }
            
            //判断用户编号是否为空
            $add_uid = db('project')->where(['project_no'=>$post['project_no']])->find();
            if ($add_uid) {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '项目编号存在了';
                $data['url'] = 'admin/Hunanconstr/project_add';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data['project_no'] = $post['project_no'];
            }
            
            //判断公司ID是否为空
            $company_id = db('company')->where(['id'=>$post['company_id']])->find();
            if ( ! $company_id) {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '公司ID不存在';
                $data['url'] = 'admin/Hunanconstr/project_add';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data['company_id'] = $post['company_id'];
                $data['item_name'] = $company_id['company_name'];
            }
            //随机生产邀请码
            $code = rand(0, 99999);
            //判断生邀请码是否存在
            $code_num = true;
            while ($code_num) {
                $arr = db('project')->where(['code'=>$code])->find();
                if (empty($arr)) {
                    $code_num = false;
                }
            }
            $data['code'] = 88888;
            $data['project_name'] = $post['project_name'];
            $data['plan_output_total'] = $post['plan_output_total'];
            $data['total_area'] = $post['total_area'];
            $data['create_time'] = time();
            $data['update_time'] = time();
            $data['can_count'] = $post['can_count'];
            $data['project_status'] = $post['project_status'];
            $data['review_status'] = 1;
            //$data['review_status'] = $post['review_status'];
            //图片上传
            $file = new \app\admin\logic\File();
            $img = $file->uploadFileAdmin($admin_id);
            
            if ( ! empty($img)) {
                $data['project_model'] = IMG_ADMIN . $img;
            }
            
            $resInsertId = db('project')->insertGetId($data);
            
            if ($resInsertId > 0) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data', $data);
                
                // 邀请码处理
                // 如果是集团领导增加绑定记录
                $field = 'user_id, head_img, name, tel, position, type, code';
                
                $leaderGroup = db('wx_jg_bound')->field($field)
                    ->where(['type'=>['in', '1,2']])
                    ->group('user_id')
                    ->select();
                
                if ($leaderGroup) {
                    $leaderData = [];
                    
                    foreach ($leaderGroup as $key=>$value) {
                        $leaderData[$key] = $value;
                        $leaderData[$key]['item_id'] = $resInsertId;
                        $leaderData[$key]['create_time'] = $this->timeStamp;
                    }
                    
                    db('wx_jg_bound')->insertAll($leaderData);
                }
                
                // 如果是分公司领导该公司下增加绑定记录
                $field = 'b.user_id, b.head_img, b.name, b.tel, b.position, b.type, b.code';
                                        
                $leaderFiliale = db('wx_jg_bound')
                    ->field($field)
                    ->alias('b')
                    ->join('project p', 'b.item_id = p.id')
                    ->where(['b.type'=>3, 'p.company_id'=>$post['company_id']])
                    ->group('b.user_id')
                    ->select();
                
                if ($leaderFiliale) {
                    $filialeData = [];
                    
                    foreach ($leaderFiliale as $key=>$value) {
                        $filialeData[$key] = $value;
                        $filialeData[$key]['item_id'] = $resInsertId;
                        $filialeData[$key]['create_time'] = $this->timeStamp;
                    }
                    
                    db('wx_jg_bound')->insertAll($filialeData);
                }
                
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        
        //公司名称
        $res = db('company')->field('id,company_name')->where("status = 1 and state = 1")->select();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    // 获取省
    public function getProvince()
    {
        $res = db('area')->field('id, name')->where(['level'=>1])->select();
    }

    public function getCity($id)
    {
        $res = db('area')->where(['pid'=>$id])->select();
    }
    
    //项目状态编辑
    public function project_edit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            
            $validate = new Validate(['plan_finish_time'=>'date'], ['plan_finish_time'=>'计划竣工时间格式错误']);
            
            if ( ! $validate->check($post)) {
                $this->error($validate->getError());
            }
            
            $company = db('company')->where(['id'=>$post['company_id']])->find();
            
            $data['company_id'] = $company['id'];
            $data['project_name'] = $post['project_name'];
            $data['project_no'] = $post['project_no'];
            $data['item_name'] = $company['company_name'];
            //$data['construction_unit'] = $post['item_name'];
            $data['can_count'] = $post['can_count'];
            
            //图片上传
            $file = request()->file('image');
            
            if ( ! empty($file)) {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    $thumb = 'uploads' . '/' . $info->getSaveName();
                    $data['project_model'] = $thumb;
                }
            }
            if ( ! empty($post['plan_finish_time'])) {
                $data['plan_finish_time'] = $post['plan_finish_time'];
            }
            
            $data['project_status'] = $post['project_status'];
            $data['pact_status'] = $post['pact_status'];
            $data['start_work'] = $post['start_work'];
            $data['update_time'] = time();
            $res = db('project')->where(['id'=>$id])->update($data);
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->success('操作失败');
            }
        }
        
        $res = db('project')->where(['id'=>$id])->find();
        
        //项目
        $mst = db('wx_jg_event')->where(['item_id'=>$id, 'event_name'=>'开工日期'])->find();
        if ($mst and $mst['event_time'] != '请选择日期') {
            $res['event_time'] = $mst['event_time'];
        } else {
            $res['event_time'] = '';
        }
        
        // 查询市
        $citys = db('area')->where(['pid'=>430000])->select();
        
        //公司名称
        $rest = db('company')->field('id,company_name')->where("status = 1 and state = 1")->select();
        $this->assign('rest', $rest);
        
        $this->assign('res', $res);
        $this->assign('citys', $citys);
        return $this->fetch();
    }
    
    //项目删除
    public function project_del($id)
    {
        $res = db('project')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //项目筛选
    public function ProjectSearch()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $where['a.company_name|b.project_name'] = ['like', '%' . $post['search'] . '%'];
            $res = db('company')->alias('a')
                ->join('project b', 'a.id = b.company_id')
                ->where($where)
                ->select();
            //获取统计数量
            $where = [];
            $where['b.company_name|a.project_name'] = ['like', '%' . $post['search'] . '%'];
            $arr = db('project')->alias('a')
                ->join('company b', 'b.id = a.company_id', 'left')
                ->field('count(a.id) as number,sum(a.plan_output_total) as plan_output_total,sum(a.total_area) as total_area')
                ->where($where)
                ->where(['a.review_status'=>1])
                ->find();
            
            $this->assign('res', $res);
            $this->assign('arr', $arr);
            return $this->fetch();
        }
    }
    
    //项目详情列表
    public function project_cat_list($id)
    {
        //项目信息
        $res = db('project')->where(['id'=>$id])->find();
        
        if (empty($res['engin_time'])) {
            $res['engin_time'] = time();
        }
        
        //获取开工时间
        $mst = db('wx_jg_event')->where(['item_id'=>$id, 'event_name'=>'开工日期'])->find();
        if ($mst and $mst['event_time'] != '请选择日期') {
            $res['event_time'] = $mst['event_time'];
        } else {
            $res['event_time'] = '待定';
        }
        //城市
        $arrs = db('area')->where(['id'=>$res['province_id']])->find();
        $str = db('area')->where(['id'=>$res['city_id']])->find();
        $res['province_id'] = $arrs['name'];
        $res['city_id'] = $str['name'];
        //成员信息
        $arr = db('wx_jg_bound')->where(['item_id'=>$id])->select();
        //云相册
        $img = db('wx_jg_album')->where(['item_id'=>$id])->select();
        foreach ($img as $k=>$v) {
            $img[$k]['image'] = json_decode($v['image']);
        }
        
        $this->assign('img', $img);
        $this->assign('arr', $arr);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //项目详情-项目修改
    public function project_cat_edit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            
            $data['province_id'] = $post['province_id'];
            $data['city_id'] = $post['city_id'];
            $data['address'] = $post['address'];
            //$data['construction_unit'] = $post['construction_unit'];
            $data['design_unit'] = $post['design_unit'];
            $data['contract_unit'] = $post['contract_unit'];
            $data['supervisor_unit'] = $post['supervisor_unit'];
            $data['total_area'] = $post['total_area'];
            $data['finished_output'] = $post['finished_output'];
            $data['finished_area'] = $post['finished_area'];
            $data['recovery_money'] = $post['recovery_money'];
            $data['renovation_area'] = $post['renovation_area'];
            $data['update_time'] = time();
            $data['lat'] = $post['lat'];
            $data['lng'] = $post['lng'];
            
            $res = db('project')->where(['id'=>$id])->update($data);
            
            if ($res) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }
    
    //成员信息
    public function contacts($id)
    {
        //dump($id);die;
        $res = db('wx_jg_bound')->alias('a')
            ->join('applet_user b', 'a.user_id = b.id')
            ->field('a.id,a.head_img,a.name,a.tel,a.position,b.country,b.province,b.city')
            ->where(['a.item_id'=>$id, 'b.admin_id'=>$this->accountId])
            ->order('a.type asc')
            ->select();
        //dump($res);die;
        $this->assign('res', $res);
        $this->assign('user_id', $id);
        return $this->fetch();
    }
    
    //成员删除
    public function user_del($id, $user_id)
    {
        $res = db('wx_jg_bound')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = url('admin/Hunanconstr/contacts', ['id'=>$user_id]);
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = url('admin/Hunanconstr/contacts', ['id'=>$user_id]);
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //成员编辑
    public function user_edit($id, $user_id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['name'] = $post['name'];
            $data['tel'] = $post['tel'];
            if ( ! empty($post['position_name'])) {
                //获取职位信息
                $arr = db('wx_jg_framework')->where(['position_name'=>$post['position_name']])->find();
                $data['position'] = $post['position_name'];
                $data['position_id'] = $arr['id'];
                $data['type'] = $arr['type'];
            }
            $res = db('wx_jg_bound')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = url('admin/Hunanconstr/contacts', ['id'=>$user_id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = url('admin/Hunanconstr/contacts', ['id'=>$user_id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        $res = db('wx_jg_bound')->where(['id'=>$id])->find();
        //职位
        $position = db('wx_jg_framework')->select();
        $this->assign('position', $position);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //绑定用户
    public function BundEdit($user_id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            //判断是否已经绑定过
            $all = db('wx_jg_bound')->where(['user_id'=>$post['id'], 'item_id'=>$user_id])->find();
            
            if ($all) {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '当前用户已经绑定过此项目';
                $data['url'] = url('admin/Hunanconstr/contacts', ['id'=>$user_id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
            
            //获取绑定用户信息
            $res = db('applet_user')->field('avatarurl,nickname')->where(['id'=>$post['id']])->find();
            
            //获取职位信息
            $arr = db('wx_jg_framework')->where(['position_name'=>$post['position_name']])->find();
            
            $data['item_id'] = $user_id;
            
            $data['position'] = $arr['position_name'];
            $data['type'] = $arr['type'];
            $data['user_id'] = $post['id'];
            $data['head_img'] = $res['avatarurl'];
            $data['name'] = $res['nickname'];
            $data['code'] = 88888;
            $data['create_time'] = time();
            
            $str = db('wx_jg_bound')->insert($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = url('admin/Hunanconstr/contacts', ['id'=>$user_id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = url('admin/Hunanconstr/contacts', ['id'=>$user_id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        //获取所有职位
        $res = db('wx_jg_framework')->select();
        $this->assign('res', $res);
        $this->assign('user_id', $user_id);
        return $this->fetch();
    }
    
    //结构列表
    public function StructureList()
    {
        $res = db('wx_jg_structure')->select();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //新增结构
    public function StructureAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['structure_name'] = $post['structure_name'];
            $data['create_time'] = time();
            $res = db('wx_jg_structure')->insert($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/StructureList';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/StructureList';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        
        return $this->fetch();
    }
    
    //结构编辑
    public function StructureEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['structure_name'] = $post['structure_name'];
            $data['create_time'] = time();
            $res = db('wx_jg_structure')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/StructureList';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/StructureList';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_structure')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        
        return $this->fetch();
    }
    
    //结构删除
    public function StructureDel($id)
    {
        $res = db('wx_jg_structure')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/StructureList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/StructureList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //建造记录
    public function framed_Data()
    {
        $project_id = input('id');
        $this->assign('id', $project_id);
        
        #参数错误
        if (empty($project_id)) {
            $this->error('参数错误');
        }
        
        //$seek = empty(input('seek')) ? '': input('seek');
        
        $where = [];
        
        if ($seek) {
            $where['ban_number'] = $seek;
        }
        
        $res = db('wx_jg_record')->where("item_id='{$project_id}'")
            ->where($where)
            ->order('ban_number asc')
            ->order('create_time asc')
            ->paginate($this->page, false, array('query'=>['seek'=>$seek, 'id'=>$project_id]));
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //编辑建造记录
    public function framedData_Edit(Request $request)
    {
        $sk = $this->accountId;
        $project_id = input('project_id');
        $this->assign('project_id', $project_id);
        
        #参数错误
        if (empty($project_id)) {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '参数错误';
            $data['url'] = url('admin/Hunanconstr/project_list');
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
        
        if (request()->isPost()) {
            $post = input('post.');
            
            #多图
            $mg = $this->List_img_uploads('image', $sk, $request);
            $post['image'] = $mg;
            if ( ! empty($mg)) {
                $id = input('id');
                $images = db('wx_jg_record')->where("id='{$id}'")->value('image');
                $resolve_imgs = $this->resolve_imgs($images, $mg);
                $data['image'] = $resolve_imgs;
            }
            
            $id = input('id');
            $res = db('wx_jg_record')->where("id='{$id}'")->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = url('admin/Hunanconstr/framed_Data', ['id'=>$project_id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = url('admin/Hunanconstr/framed_Data', ['id'=>$project_id]);
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        } else {
            #获取店铺信息
            $id = input('id');
            $where['id'] = $id;
            $where['item_id'] = $project_id;
            $res = db('wx_jg_record')->where($where)->find();
            $this->assign('res', $res);
            
            #轮播图
            $imges = $res['image'];
            $imges1 = '';
            $imges2 = '';
            $imges3 = '';
            if ( ! empty($imges)) {
                //转换格式
                $imges = json_decode($imges, true);
                foreach ($imges as $k=>$v) {
                    if (isset($imges[0])) {
                        $imges1 = $imges[0];
                    } else {
                        $imges1 = '';
                    }
                    if (isset($imges[1])) {
                        $imges2 = $imges[1];
                    } else {
                        $imges2 = '';
                    }
                    if (isset($imges[2])) {
                        $imges3 = $imges[2];
                    } else {
                        $imges3 = '';
                    }
                }
            }
            $this->assign('imges1', $imges1);
            $this->assign('imges2', $imges2);
            $this->assign('imges3', $imges3);
            
            return $this->fetch();
        }
    }
    
    //建造记录删除
    public function framedData_Del($id)
    {
        $res = db('wx_jg_record')->where(['id'=>$id])->delete();
        
        if ($res) {
            //删除圈圈图片
            db('wx_jg_coworker')->where(['type_id'=>$id, 'type'=>2])->delete();
            
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //大事记
    public function memorabilia($id)
    {
        $project_id = input('id');
        $this->assign('id', $project_id);
        
        #参数错误
        if (empty($project_id)) {
            $this->error('参数错误');
        }
        
        $res = db('wx_jg_event')->where("item_id='{$project_id}'")->order('sort asc, create_time')->paginate($this->page, false, array('query'=>['id'=>$project_id]));
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        $this->assign('cid', $id);
        return $this->fetch();
    }
    
    // 清除大事记 - 图片/日期
    public function memorabiliaDel($id)
    {
        $res = db('wx_jg_event')->where(['id'=>$id])->find();
        
        if ( ! $res) {
            $this->error('记录不存在');
        }
        
        $update = [];
        $update['event_img'] = '';
        $update['event_time'] = '';
        $update['state'] = 0;
        
        $res = db('wx_jg_event')->where(['id'=>$id])->update($update);
        
        $this->success('清除记录成功');
    }
    
    //大事记图片审核
    public function thumb_examine($id)
    {
        $res = db('wx_jg_event')->where(['id'=>$id])->update(['state'=>1]);
        if ($res) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    
    //大事记图片修改
    public function thumb_de($id)
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            //图片上传
            if (isset($_FILES['image']) &&  ! empty($_FILES['image'])) {
                $file = request()->file('image');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    $thumb = 'uploads' . '/' . $info->getSaveName();
                    $data['event_img'] = $thumb;
                }
            }
            $res = db('wx_jg_event')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data', $data);
                echo $thumb;
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
    }
    
    //云相册列表
    public function CloudAlbum($id)
    {
        $res = db('wx_jg_album')->where(['item_id'=>$id])->order('time_new desc')->paginate($this->page, false);
        foreach ($res as $k=>$v) {
            $v['image'] = json_decode($v['image']);
            $res->offsetSet($k, $v);
        }
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //云相册编辑
    public function CloudAlbumEdit()
    {
        if (request()->isPost()) {
            $post = input('post.');
            dump($post['id']);
            die();
        }
        $res = db('wx_jg_album')->where(['id'=>$id])->find();
        $res['image'] = json_decode($res['image']);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //云相册删除
    public function CloudAlbumDel($id)
    {
        $res = db('wx_jg_album')->where(['id'=>$id])->delete();
        if ($res) {
            //删除圈圈照片
            db('wx_jg_coworker')->where(['type'=>1, 'type_id'=>$id])->delete();
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/project_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }

    /**
     * 刷刷管理
     */
    //公告列表
    public function NoticeList()
    {
        $res = db('wx_jg_article')->order('create_time desc')->select();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //公告增加
    public function NoticeAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['title'] = $post['title'];
            $data['countet'] = $post['countet'];
            $data['create_time'] = time();
            $res = db('wx_jg_article')->insert($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/NoticeList';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/NoticeList';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        return $this->fetch();
    }
    
    //公告修改
    public function NoticeEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['title'] = $post['title'];
            $data['countet'] = $post['countet'];
            $data['create_time'] = time();
            $res = db('wx_jg_article')->where(['id'=>$id])->update($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/NoticeList';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/NoticeList';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_article')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //公告删除
    public function NoticeDel($id)
    {
        $res = db('wx_jg_article')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/NoticeList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/NoticeList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //刷刷图片管理
    public function ThumbList()
    {
        $res = db('wx_jg_coworker')->paginate($this->page, false);
        
        $page = $res->render();
        $this->assign('page', $page);
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //周报
    public function Weekly()
    {
        if (request()->isPost()) {
            $post = request()->only(['day', 'city', 'county', 'resettlement'], 'post');
            
            $res = db('wx_jg_weekly')->where(['id'=>1])->update($post);
            
            $this->success('操作成功');
        }
        
        $res = db('wx_jg_weekly')->find();
        
        $day1 = time();
        $day2 = 1501516800;
        $res['day'] = intval(($day1 - $day2) / 86400);
        
        if ($res['day'] > 0) {
            db('wx_jg_weekly')->where(['id'=>1])->update(['day'=>$res['day']]);
        }
        
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //检查列表
    public function RectificationList()
    {
        $res = db('wx_jg_application')->where(['sty'=>0])->field('id,item_id,item_name,nature,date,results,image,sty')->paginate($this->page, false);
        foreach ($res as $k=>$v) {
            $v['image'] = json_decode($v['image']);
            $res->offsetSet($k, $v);
        }
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //检查删除
    public function RectificationDel($id)
    {
        $res = db('wx_jg_application')->where(['id'=>$id])->delete();
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/RectificationList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/RectificationList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //整改列表
    public function RectList()
    {
        $res = db('wx_jg_applics')->alias('a')
            ->join('wx_jg_application b', 'a.cid = b.id', 'left')
            ->field('a.cid,a.id,a.describe,a.img,a.create_time,b.item_name')
            ->paginate($this->page, false);
        foreach ($res as $k=>$v) {
            $v['img'] = json_decode($v['img']);
            $res->offsetSet($k, $v);
        }
        
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //整改删除
    public function Rectdel($id, $cid)
    {
        $res = db('wx_jg_applics')->where(['id'=>$id])->delete();
        if ($res) {
            //更新检查状态为 未整改
            db('wx_jg_application')->where(['id'=>$cid])->update(['sty'=>0]);
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/RectList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/RectList';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }

    /**
     * 使用说明
     */
    //说明内容
    public function Content()
    {
        $res = db('wx_jg_explain_content')->where("type = 1")->select();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //说明编辑
    public function ContentEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['title'] = $post['title'];
            $data['title1'] = $post['title1'];
            $data['title2'] = $post['title2'];
            $data['url'] = $post['url'];
            $data['create_time'] = time();
            $res = db('wx_jg_explain_content')->where(['id'=>$id])->update($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Content';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Content';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_explain_content')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //联系人
    public function Contactslist()
    {
        $res = db('wx_jg_explain_contacts')->limit(100)->select();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //新增联系人
    public function ContactsAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['name'] = $post['name'];
            $data['tel'] = $post['tel'];
            $data['weixin'] = $post['weixin'];
            $data['create_time'] = time();
            $res = db('wx_jg_explain_contacts')->insert($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Contactslist';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Contactslist';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        return $this->fetch();
    }
    
    //编辑联系人
    public function ContactsEdit($id)
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['name'] = $post['name'];
            $data['tel'] = $post['tel'];
            $data['weixin'] = $post['weixin'];
            $data['create_time'] = time();
            $res = db('wx_jg_explain_contacts')->where(['id'=>$id])->update($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Contactslist';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Contactslist';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        
        $res = db('wx_jg_explain_contacts')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //删除联系人
    public function ContactsDel($id)
    {
        $res = db('wx_jg_explain_contacts')->where("id = $id")->delete();
        
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/Contactslist';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/Contactslist';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
    
    //首页公告
    public function Notice()
    {
        if (request()->isPost()) {
            $post = request()->only(['title', 'notice_on', 'delay_on'], 'post');
            $data['title'] = $post['title'];
            $data['notice_on'] = $post['notice_on'];
            $data['delay_on'] = $post['delay_on'];
            $res = db('wx_jg_explain_content')->where("type = 2")->update($data);
            
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/Notice';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/Notice';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
        $res = db('wx_jg_explain_content')->where("type = 2")->find();
        $this->assign('res', $res);
        return $this->fetch();
    }

    /**
     * 系统设置
     */
    public function Binding()
    {
        $res = db('wx_jg_code')->select();
        
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    //项目模型图片编辑
    public function thumb_model($id)
    {
        $admin_id = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            //图片上传
            if (isset($_FILES['image']) &&  ! empty($_FILES['image'])) {
                $file = request()->file('image');
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if ($info) {
                    $thumb = 'http://bike.wei-yong.com/' . 'uploads' . '/' . $info->getSaveName();
                    $data['project_model'] = $thumb;
                }
            }
            $res = db('project')->where(['id'=>$id])->update($data);
            if ($res) {
                $data = [];
                $data['status'] = 1;
                $data['info'] = '操作成功';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data', $data);
                echo $thumb;
            } else {
                $data = [];
                $data['status'] = 0;
                $data['info'] = '操作失败';
                $data['url'] = 'admin/Hunanconstr/project_list';
                $this->assign('data', $data);
                return $this->fetch('statuspage');
            }
        }
    }

    public function companyFinance()
    {
        $company = db('company')->field('company_name, code')->limit(100)->select();
        
        $this->assign('res', $company);
        return $this->fetch();
    }
    
    //解除财务
    public function Finance($id)
    {
        //获取公司code
        $company = db('company')->field('code')->where("id = $id")->find();
        //删除绑定人
        $res = db('wx_jg_binding')->where(['type'=>2, 'code'=>$company['code']])->delete();
        
        if ($res) {
            $data = [];
            $data['status'] = 1;
            $data['info'] = '操作成功';
            $data['url'] = 'admin/Hunanconstr/company_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        } else {
            $data = [];
            $data['status'] = 0;
            $data['info'] = '操作失败';
            $data['url'] = 'admin/Hunanconstr/company_list';
            $this->assign('data', $data);
            return $this->fetch('statuspage');
        }
    }
}